package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import uteis.Constantes;
import model.Bancos;

public class BancosJDBCCadastro implements BancosCadastroDAO {

	Connection conexao;

	public BancosJDBCCadastro(Connection conexao) {
		this.conexao = conexao;
	}

	public String salvar(Bancos bancos) {
		String sqlquery = "insert into BANCO(NMBANCO, NMREDUZIDO, USUINCLUSAO, COMPINCLUSAO, USUALTERACAO, COMPALTERACAO, DTINCLUSAO, DTALTERACAO, CDEMPRESA)"
				+ " values(?,?,?,?,?,?,?,?,?);";

		PreparedStatement pstmt;
		try {

			pstmt = this.conexao.prepareStatement(sqlquery);
			pstmt.setString(1, bancos.getNmbanco());
			pstmt.setString(2, bancos.getNmreduzidobanco());
			pstmt.setString(3, bancos.getUsuinclusaobanco());
			pstmt.setString(4, bancos.getCompinclusaobanco());
			pstmt.setString(5, bancos.getUsualteracaobanco());
			pstmt.setString(6, bancos.getCompalteracaobanco());
			pstmt.setDate(7, new java.sql.Date(bancos.getDtinclusaobanco()
					.getTime()));
			// pstmt.setDate(8, new java.sql.Date(
			// bancos.getHrinclusaobanco().getTime()));
			pstmt.setDate(8, new java.sql.Date(bancos.getDtalteracaobanco()
					.getTime()));
			// pstmt.setDate(10, new java.sql.Date(
			// bancos.getHralteracaobanco().getTime()));
			pstmt.setString(9, Constantes.cdempresas);
			// System.out.println("BANCOS comando sql:"+sqlquery);
			pstmt.execute();

			return ("success");

		} catch (Exception e) {

			e.printStackTrace();
			return ("error");
		}
	}

	public boolean localizarBanco(String nmbanco) {
		String sqlquery = "SELECT NMBANCO FROM BANCO WHERE (NMBANCO like '"
				+ nmbanco + "' and CDEMPRESA = " + Constantes.cdempresas
				+ ") ORDER BY NMBANCO ASC;";
		try {
			String valor = "";
			java.sql.Statement stmt = conexao.createStatement();
			ResultSet rs = null;
			rs = stmt.executeQuery(sqlquery);
			while (rs.next()) {
				valor = rs.getString("NMBANCO");
			}
			if (valor.equals("")) {
				return (false);
			} else {
				return (true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return (false);
		}
	}

	public ResultSet ResultBancos() {

		ResultSet value = null;
		String sql = "select CDBANCO, NMBANCO from BANCO;";
		try {
			java.sql.Statement stmt = conexao.createStatement();
			value = stmt.executeQuery(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}

	public void editar(Bancos bancos) {
	}

	public void excluir(Bancos bancos) {
	}

	public List<Bancos> ListarBancosT() {
		return null;
	}

}